<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .box {
            width: 900px;
            height: 510px;
            background-color: #11212F;
            box-shadow: 0 0 8px black;
            cursor: pointer;
            position: relative;
            margin: auto;
            overflow: hidden;

        }

        .box .tu {
            width: 2700px;
            position: absolute;
            left: 0;
            height: 340px;

            transition: 0.2s linear;

        }

        .tu>img {
            float: left;
        }

        .box .tu img {
            width: 900px;
            height: 505px;

        }

        .zuo {
            width: 45px;
            height: 108px;
            background: rgba(0, 0, 0, 0.52);
            position: absolute;
            top: 35%;
            left: 0;
            cursor: pointer;
            border: none;
            z-index: 99;
            opacity: 0.5;
            border: none;
            display: none;
        }

        .zuo p {
            width: 23px;
            height: 36px;
            margin: 0 auto;
            margin-top: 35px;
            color: white;
            font-family: 宋体;
            font-size: 28px;

        }

        .zuo:hover {
            background-image: linear-gradient(90deg, rgba(255, 255, 255, 0.8), transparent);
        }

        .you {
            width: 45px;
            height: 108px;
            background: rgba(0, 0, 0, 0.52);
            position: absolute;
            top: 35%;
            right: 0;
            cursor: pointer;
            display: none;
            z-index: 99;
            opacity: 0.5;
            border: none;
        }

        .you p {
            width: 23px;
            height: 36px;
            margin: 0 auto;
            margin-top: 35px;
            color: white;
            font-family: 宋体;
            font-size: 28px;

        }

        .you:hover {
            background-image: linear-gradient(-90deg, rgba(255, 255, 255, 0.8), transparent);
        }

        .box .ico {

            transform: scale(1.5);
            position: absolute;
            bottom: 25px;
            left: 400px;
        }

        .ico span {
            width: 30px;
            height: 16px;
            background-color: #496274;
            float: left;
            margin-left: 5px;
            border-radius: 4px;
            cursor: pointer;
            color: transparent;

        }

        .ico span:hover {
            background-color: rgb(161, 181, 195);
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="tu">
            <img src="./小姐姐2.jpeg" alt="">
            <img src="./小姐姐3.jpeg" alt="">
            <img src="./小姐姐5.jpeg" alt="">
        </div>
        <div class="zuo">
            <p>&lt;</p>
        </div>
        <div class="you">
            <p>></p>
        </div>
        <div class="ico">
            <span></span>
            <span></span>
            <span></span>
        </div>
    </div>



    <script>
        // 获取元素
        var spans = document.querySelector('.ico').querySelectorAll('span');
        var imgs = document.querySelector('.tu')
        var box = document.querySelector('.box')
        var zuo = document.querySelector('.zuo')
        var you = document.querySelector('.you')
        console.log(imgs);
        // 鼠标移到盒子里面左右两个按钮显示出来
        box.addEventListener('mouseover', function () {
            zuo.style.display = 'block';
            you.style.display = 'block';
            zuo.style.backgroundColor = 'black';
            you.style.backgroundColor = 'black';
            clearInterval(time); // 鼠标移到盒子内 移除计时器
        })
        // 鼠标移到盒子外面左右两个按钮隐藏起来
        box.addEventListener('mouseleave', function () {
            zuo.style.display = 'none';
            you.style.display = 'none';
            time = setInterval(timer, 2000); // 鼠标移到盒子外 创建计时器
        })
        // 点击下面的三个点 会跳转到相应的位置
        for (let i = 0; i < spans.length; i++) {
            spans[i].onclick = function () {
                var x = 900 * i;// 计算位置的量 900为一张图片的宽度
                imgs.style.transform = `translateX(${-x}px)`// 添加2d位移
            }
            spans[i].addEventListener('click', function () {
                for (let k = 0; k < spans.length; k++) { // 排他思想 下面变色前 先把之前的颜色清除为三个点原来的颜色
                    spans[k].style.backgroundColor = 'black'
                }
                spans[i].style.backgroundColor = '#A1B5C3'; // 点击时变色
                j = i; // 并把所索引的值给j，否者j还是从原来的0++，图片跟按钮的顺序会变化
            })
        }
        var j = 0; // 声明一个计数器
        you.addEventListener('click', function () {
            j++; // 点击右边的按钮j+1
            if (j > 2) j = 0;// 当j到2的时候在++时，因为右边没有图片了，就另j=2也就是最大值为2
            imgs.style.transform = `translateX(${-900 * j}px)`;
            for (let k = 0; k < spans.length; k++) {
                spans[k].style.backgroundColor = 'black';
            }
            spans[j].style.backgroundColor = '#A1B5C3';
        })

        zuo.addEventListener('click', function () {
            j--;
            if (j < 0) j = 2;
            imgs.style.transform = `translateX(${-900 * j}px)`;
            for (let k = 0; k < spans.length; k++) {
                spans[k].style.backgroundColor = 'black';
            }
            spans[j].style.backgroundColor = '#A1B5C3';
        })



        var time = setInterval(timer, 2000); // 创建一个计时器 间隔为2s 并调用timer函数
        function timer() {
            j++;
            if (j > 2) {
                j = 0;
            };
            imgs.style.transform = `translateX(${-900 * j}px)`;
            for (let k = 0; k < spans.length; k++) {
                spans[k].style.backgroundColor = 'black'

            }
            spans[j].style.backgroundColor = '#A1B5C3';
        }
     

    </script>
</body>

</html>